package ExtraDemo.demo01;

public class Cooker implements Runnable {
    /*生产者步骤
        1.判断桌子上有没有汉堡包
        2.如果有则等待
        3.如果没有则生产汉堡包
        4.生产之后，汉堡包数量加1，同时唤醒消费者消费
     */
    Desk desk = null;

    public Cooker(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                while (desk.getNum() == 10) {
                    try {
                        desk.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int num = desk.getNum();
                desk.setNum(++num);
                System.out.println("厨师正在生产汉堡包,现在的个数为" + desk.getNum());
                if (desk.getNum() > 0) {
                    desk.getLock().notifyAll();
                }
            }
        }
    }
}
